package com.clothing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clothing.dao.LeaveMessageDao;
import com.clothing.dao.LeaveMessageReplyDao;
import com.clothing.entity.LeaveMessage;
import com.clothing.entity.LeaveMessageReply;
import com.clothing.entity.User;
import com.clothing.service.LeaveMessageReplyService;
import com.clothing.utils.page.Filter;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;
import com.clothing.utils.page.Filter.Operator;

/**
 * @ClassName: LeaveMessageReplyServiceImpl 
 * @Description: TODO(Service) 
 * @author Charles.Tao 
 * @date 2017年9月6日 下午3:43:34 
 * @version 1.0
 */
@Service
public class LeaveMessageReplyServiceImpl extends BaseServiceImpl<LeaveMessageReply, Long> implements LeaveMessageReplyService
{
    @Autowired
    private LeaveMessageReplyDao leaveMessageReplyDao;
    
    @Autowired
    private LeaveMessageDao leaveMessageDao;
    
    @Override
    public void sendMessage(LeaveMessage leaveMessage, boolean isNew, Long userId, String content)
    {
        leaveMessage.setLastMessage(content);
        
        if (isNew)
        {
            if (userId.equals(leaveMessage.getSmallUser().getId()))
            {
                leaveMessage.setSmallUnreadQuantity(1);
                leaveMessage.setBigUnreadQuantity(0);
            }
            else
            {
                leaveMessage.setSmallUnreadQuantity(0);
                leaveMessage.setBigUnreadQuantity(1);
            }
            
            leaveMessageDao.persist(leaveMessage);
        }
        else
        {
            if (userId.equals(leaveMessage.getSmallUser().getId()))
            {
                leaveMessage.setSmallUnreadQuantity(leaveMessage.getSmallUnreadQuantity() + 1);
            }
            else
            {
                leaveMessage.setBigUnreadQuantity(leaveMessage.getBigUnreadQuantity() + 1);
            }
            
            leaveMessageDao.merge(leaveMessage);
        }
        
        User bigUser = leaveMessage.getBigUser();
        User smallUser = leaveMessage.getSmallUser();
        LeaveMessageReply leaveMessageReply = new LeaveMessageReply();
        leaveMessageReply.setContent(content);
        leaveMessageReply.setLeaveMessage(leaveMessage);
        leaveMessageReply.setIsRead(Boolean.FALSE);
        
        if (bigUser.getId().equals(userId))
        {
            leaveMessageReply.setSender(smallUser);
            leaveMessageReply.setReceiver(bigUser);
        }
        else
        {
            leaveMessageReply.setSender(bigUser);
            leaveMessageReply.setReceiver(smallUser);
        }
        
        leaveMessageReplyDao.persist(leaveMessageReply);
    }

    @Override
    public Page<LeaveMessageReply> findLeaveMessageDetail(Long leaveMessageId, Pageable pageable)
    {
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("leaveMessage.id", Operator.eq, leaveMessageId));
        pageable.setFilters(filters);
        
        return findPage(pageable);
    }

    @Override
    public List<LeaveMessageReply> findUnreadLeaveMessageDetail(Long leaveMessageId)
    {
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("leaveMessage.id", Operator.eq, leaveMessageId));
        filters.add(new Filter("isRead", Operator.eq, Boolean.FALSE));
        
        return findList(null, filters, null);
    }

    @Override
    public List<LeaveMessageReply> findNewMessage(Long leaveMessageId, Long lastMessageId)
    {
        return leaveMessageReplyDao.findNewMessage(leaveMessageId, lastMessageId);
    }
}
